
class Admin < Controller
  plug 'admin'

  # every action under Admin controller is protected
  def go e
    if users.current_user
      super
    else
      login e
    end
  end

  def initialize
    puts "why doesn't this get called?!"
  end

  def users
    if @users
      @users
    else
      require 'appengine-apis/users'
      @users ||= Module.const_get('AppEngine').const_get('Users')
    end
  end

  def index e
    "<br /><center><img src=\"/bublifuk/lolcats/wellcome_back.jpg\"></center>"
  end

  def login e
    url = users.create_login_url(e.url)
    "<br /><center><img src=\"/bublifuk/lolcats/cat-limits-your-computer-access.jpg\"><h1>Please <a href=\"#{url}\">log in</a>.</h1></center>"
  end

  def logout e
    if users.logged_in?
      '<a href="'+users.create_logout_url(e.link 'index')+'">logout</a>'
    else
      'You are already logged out.'
    end
  end

  def structure e
    Structure.go(e)
  end

  def content e
    Content.go e
  end

  def layout e
    Creator.go e
  end

  def templates e
    Templates.go e
  end

  def designer e
    #VisualEditor.go e
    '<iframe src="/admin/ve/" id="visual_editor_iframe"></iframe>'
  end

  def layouts e
    Layouts.go(e)
  end


  def ve e
    e.respond :html => VisualEditor.go(e)
  end

  def app_tests e
    RunTests.new.app_tests(e)
  end

  def fw_tests e
    RunTests.new.fw_tests(e)
  end

  def terminal e
    Terminal.go e
  end


  def import e
    ImportStuff.new.go(e)
  end

  def logs e
    DisplayLogs.go(e)
  end




  class Layout
    def self.render e

      top_links =
        '
          <a href="/admin/content">Obsah</a>
          <a href="/admin/templates">Druhy obsahu</a>
          <!--<a href="/admin/structure">Struktura</a>-->
          <span class="separator"></span>
          <a href="/admin/designer">Editor</a>
          <a href="/admin/layouts">Layouty</a>
        '

      if user = Admin.instance.users.current_user
        userline = user.nickname + ' | <a href="/admin/logout/">Odhlášení</a>'

        if Admin.instance.users.admin?
          top_links << '
            <span class="separator"></span>
            <a href="/admin/terminal">Terminál</a>
            <a href="/admin/app_tests">AppTesty</a>
            <a href="/admin/fw_tests">FwTesty</a>
<!--
            <span class="separator"></span>
            <a href="/admin/import">Import</a>
            <a href="/admin/logs">Logy</a>
-->
            '
        end
      end


      <<HTML
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
  <title>Admin</title>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

  <!-- good old yui 2.8 -->
    <!--<link rel="stylesheet" href="/bublifuk/js/yui2/yui.css" type="text/css" media="screen" />-->
    <script type="text/javascript" src="/bublifuk/js/yui2/yui.js"></script>
  <!-- end yui -->

  <script type="text/javascript" src="/bublifuk/js/yui3/yui-combo.js"></script>

  <link rel="stylesheet" href="/bublifuk/admin/style.css" type="text/css" media="screen" />
  <script type="text/javascript" src="/bublifuk/js/thing.js"></script>


</head>
<body class="yui-skin-sam">
  <div id="layout_table" cellspacing="0" cellpadding="1">
    <div id="top_toolbar">
      <div>
        <div class="toolbar_head">
          #{top_links}
        </div>

        <div id="userline">
          #{userline}
        </div>
      </div>
    </div>
    <tdiv id="bottom_toolbar" valign="top">
      <div>
        #{e.content}
      </div>
    </div>
  </div>
  
</body>
</html>
HTML
    end
  end

  layout Layout

end

